package org.foreningsgatan.mercenaries.rendering;

import org.foreningsgatan.mercenaries.Configuration;
import org.foreningsgatan.mercenaries.model.terrain.TerrainMap;
import org.foreningsgatan.mercenaries.model.terrain.TerrainTile;
import org.foreningsgatan.modular.lightsources.LightMap;
import org.foreningsgatan.modules.glcore.graphics.GLGraphics;

/**
 * Copyright (c) 2008 Joakim Back
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 * 
 * @author Joakim Back
 *
 */
public class TerrainRenderer implements Renderer {
	private TerrainMap mTerrainMap;

	private LightMap mLightMap;

	public TerrainRenderer() {
	}
	
	public void update(TerrainMap pTerrainMap, LightMap pLightMap) {
		mTerrainMap = pTerrainMap;
		mLightMap = pLightMap;
	}
	
	public void render(GLGraphics g, TileClippingRect pRect,
			ScrollPosition pPos, boolean hasScrolled) {
		float tw = (float) Configuration.sTileWidth
				/ Configuration.sTerrainTextureWidth;
		float th = (float) Configuration.sTileHeight
				/ Configuration.sTerrainTextureHeight;

		g.enableMultiTexturing();

		for (int iy = pRect.getVerticalFirst(); iy < pRect.getVerticalLast() + 3; iy++) {
			for (int ix = pRect.getHorizontalFirst(); ix < pRect
					.getHorizontalLast() + 3; ix++) {
				// Fetch tile

				TerrainTile tile = mTerrainMap.get(ix, iy);

				if (tile == null) {
					continue;
				}

				// Calculate window coordinates

				float x = ((float) ix * Configuration.sTileWidth - pPos.getX())
						- Configuration.sTileWidth / 2;
				float y = ((float) iy * Configuration.sTileHeight - pPos
						.getY())
						- Configuration.sTileHeight / 2;

				// Calculate texture coordinates

				float tx = ix * tw;
				float ty = iy * th;

				g.drawMultiTexture(x, y, Configuration.sTileWidth,
						Configuration.sTileHeight, tile.getTextureId1(), tile
								.getTextureId2(), tile.isPrimary(),
						new float[] { tx, ty, tw, th }, new float[][] {
								mLightMap.get(ix - 1, iy - 1).getRGB(),
								mLightMap.get(ix, iy - 1).getRGB(),
								mLightMap.get(ix - 1, iy).getRGB(),
								mLightMap.get(ix, iy).getRGB() });
			}
		}

		g.disableMultiTexture();
	}
}
